﻿using System.Collections;
using Eneta.Portal.Forum.Core.Data;
using Eneta.Portal.Forum.Core.Domain.Collections;
using Eneta.Portal.Forum.Core.Domain.Entities;
using Eneta.Portal.Forum.Core.Service.Application;

namespace Eneta.Portal.Forum.Core.Domain.Repositories
{
    public class ForumRepository
    {
        // Fields
        private readonly ForumDao _dao = new ForumDao();

        // Methods
        public ForumCollection FindByCategoryId(int id)
        {
            return _dao.FindByCategoryId(id);
        }

        public Entities.Forum FindByTopicId(int topicId)
        {
            Topic byId = RepositoryRegistry.TopicRepository.GetById(topicId);
            return GetById(byId.ForumId);
        }

        public ForumCollection GetAll()
        {
            return _dao.GetAll();
        }

        public Entities.Forum GetById(int id)
        {
            return _dao.GetById(id);
        }

        public Hashtable GetPermissionsForForum(int id)
        {
            return _dao.GetPermissionsForForum(id);
        }

        public void IncreasePostCount(int topicId)
        {
            var byId = RepositoryRegistry.TopicRepository.GetById(topicId);
            var forum = GetById(byId.ForumId);
            forum.PostCount++;
            Save(forum);
        }

        public void IncreaseTopicCount(int id)
        {
            var byId = GetById(id);
            byId.TopicCount++;
            Save(byId);
        }

        public int Save(Entities.Forum forum)
        {
            return _dao.Save(forum);
        }

        public int SavePermissions(int groupId, int forumId, Permission permission)
        {
            return _dao.SavePermissions(groupId, forumId, permission);
        }
    }
}